<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

class CollectList
{
    public $login = false; //是否需要登录才能访问该接口
    public $title = "收藏列表ok";
    public $group = '收藏';
    public $desc = "";

    public $input = [
        'page_num' => 'int;label=分页;comment=默认为1',
        'page_size' => 'int;label=分页条数;comment=默认为5',
        'type_id' => 'int;required;label=类型id',
    ];

    public $output = [
        'page_num' => 'label=当前页码;',
        'page_size' => 'label=每页条数;',
        'page_count' => 'label=总条数;',
        'page_total' => 'label=总页数;',
        'list' => [
            [
                'id' => 'label=id;',
                'title' => 'label=标题;',
                'relate_id' => 'label=关联id;',
                'new_desc' => 'label=描述;',
                'logo_img' => 'label=logo图;',
                'type_id' => 'label=类型;comment=0园区资讯 1帖子 2政务资讯 3园区活动',
                'create_time' => 'label=创建时间;',
            ]
        ]
    ];

    public function run($param, $uid)
    {
        $page_num = $param['page_num'] ?: 1;
        $page_size = $param['page_size'] ?: 10;

        $list = M('user_collect')->where(['uid' => $uid, 'type' => $param['type_id']])->order('id desc')->page($page_num, $page_size)->select();
        $result = [];
        foreach($list as &$item){
            if($item['type'] == 0){
                $ret = M('yq_news')->where(['id' => $item['relate_id']])->find();
                $item['title'] = $ret['title'];
                $item['new_desc'] = $ret['new_desc'];
                $item['logo_img'] = $ret['logo_img'];
            }

            if($item['type'] == 2){
                $ret = M('news')->where(['id' => $item['relate_id']])->find();
                $item['title'] = $ret['title'];
                $item['new_desc'] = $ret['new_desc'];
                $item['logo_img'] = $ret['logo_img'];
            }

            if($item['type'] == 1){
                $ret = M('user_infor')->where(['id' => $item['relate_id']])->find();
                $item['title'] = $ret['title'];
                $item['new_desc'] = $ret['describe'];
                $logo_arr = explode(',', $ret['img']);
                $item['logo_img'] = $logo_arr[0];
            }

            if($item['type'] == 3){
                $ret = M('act_news')->where(['id' => $item['relate_id']])->find();
                $item['title'] = $ret['title'];
                $item['logo_img'] = $ret['logo_img'];
                $item['new_desc'] = $ret['new_desc'];
            }

            if($item['type'] == 4){
                $ret = M('service')->where(['id' => $item['relate_id']])->find();
                $item['title'] = $ret['infor_title'];
                $item['logo_img'] = $ret['logo_img'];
                $item['new_desc'] = $ret['desc_service'];
            }
        }
        $count = M('user_collect')->where(['uid' => $uid, 'type' => $param['type_id']])->count();
        return [
            'list'=> $list,
            'page_count' => $count,
            'page_total' => ceil($count / $page_size)
        ];
    }
}
